package com.lindroy.service

import com.lindroy.entity.Order
import com.lindroy.repository.OrderRepo
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.util.*

/**
 * @author Lin
 * @date 2019/10/15
 * @function
 * @Description
 */
@Service
@Transactional
class OrderService : OrderRepo {

    @Autowired
    lateinit var orderRepo: OrderRepo

    /**
     * Saves a given entity. Use the returned instance for further operations as the save operation might have changed the
     * entity instance completely.
     *
     * @param entity must not be null.
     * @return the saved entity; will never be null.
     */
    override fun <S : Order?> save(entity: S):S = orderRepo.save(entity)

    /**
     * Returns all instances of the type.
     *
     * @return all entities
     */
    override fun findAll():MutableIterable<Order> = orderRepo.findAll()

    /**
     * Deletes the entity with the given id.
     *
     * @param id must not be null.
     * @throws IllegalArgumentException in case the given `id` is null
     */
    override fun deleteById(id: Int) = orderRepo.deleteById(id)

    /**
     * Deletes the given entities.
     *
     * @param entities
     * @throws IllegalArgumentException in case the given [Iterable] is null.
     */
    override fun deleteAll(entities: MutableIterable<Order>) = orderRepo.deleteAll(entities)

    /**
     * Deletes all entities managed by the repository.
     */
    override fun deleteAll() = orderRepo.deleteAll()

    /**
     * Saves all given entities.
     *
     * @param entities must not be null.
     * @return the saved entities; will never be null.
     * @throws IllegalArgumentException in case the given entity is null.
     */
    override fun <S : Order?> saveAll(entities: MutableIterable<S>): MutableIterable<S>?  = orderRepo.saveAll(entities)

    /**
     * Returns the number of entities available.
     *
     * @return the number of entities
     */
    override fun count() = orderRepo.count()

    /**
     * Returns all instances of the type with the given IDs.
     *
     * @param ids
     * @return
     */
    override fun findAllById(ids: MutableIterable<Int>): MutableIterable<Order> =orderRepo.findAllById(ids)

    /**
     * Returns whether an entity with the given id exists.
     *
     * @param id must not be null.
     * @return true if an entity with the given id exists, false otherwise.
     * @throws IllegalArgumentException if `id` is null.
     */
    override fun existsById(id: Int) = orderRepo.existsById(id)

    /**
     * Retrieves an entity by its id.
     *
     * @param id must not be null.
     * @return the entity with the given id or Optional#empty() if none found
     * @throws IllegalArgumentException if `id` is null.
     */
    override fun findById(id: Int): Optional<Order> = orderRepo.findById(id)

    /**
     * Deletes a given entity.
     *
     * @param entity
     * @throws IllegalArgumentException in case the given entity is null.
     */
    override fun delete(entity: Order) = orderRepo.delete(entity)

}